package com.mycar.pajero.controller;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.mycar.pajero.PajeroApplication;
import com.mycar.pajero.common.IPUtil;
import com.mycar.pajero.common.ResponseInfo;
import com.mycar.pajero.entity.CarDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/")
public class HomeController {

    private static Logger logger= LoggerFactory.getLogger(PajeroApplication.class);

    @GetMapping("")
    public ResponseInfo<String> index(HttpServletRequest request){
        String ipaddress= IPUtil.getIpAddr(request);
        return ResponseInfo.success("ip " +ipaddress);
    }


    @PostMapping("testpost")
    public ResponseInfo<CarDTO> testpost(HttpServletRequest request,@RequestBody CarDTO dto){

        return ResponseInfo.success(dto);
    }

    @PostMapping("testrequest")
    public ResponseInfo<String> testrequest(HttpServletRequest request,@RequestBody CarDTO dto){
        JSONObject json = new JSONObject();
        json.put("username", "55217242@qq.com");
        json.put("password", "TmjLS.QWem93H6N");

        String result = HttpRequest.post("https://api2.bmob.cn/1/users")
                .header("Content-Type", "application/json")
                .body(json.toJSONString())
                .execute().body();
        return ResponseInfo.success(result);
    }

}
